package com.fr.web.core.service;

import com.fr.base.FRContext;
import com.fr.base.file.ConfigManager;
import com.fr.report.PageSet;
import com.fr.report.ReportPage;
import com.fr.util.Consts;
import com.fr.web.core.SessionIDInfor;
import com.fr.web.core.WebUtils;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AppletPrintService extends NoSessionIDService
{
  private static AppletPrintService APPLET_PRINT_SERVICE = null;

  public static AppletPrintService getInstance()
  {
    if (APPLET_PRINT_SERVICE == null)
      APPLET_PRINT_SERVICE = new AppletPrintService();
    return APPLET_PRINT_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return paramString.startsWith("fr_applet_");
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    if (paramString.equals("fr_applet_load"))
      dealAppletLoad(paramHttpServletRequest, paramHttpServletResponse);
    else if (paramString.equals("fr_applet_print"))
      dealAppletPrint(paramHttpServletRequest, paramHttpServletResponse);
  }

  private static void dealAppletPrint(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    paramHttpServletResponse.setContentType("application/octet-stream");
    String str = SessionDealWith.getOrGenerateSessionID(paramHttpServletRequest, paramHttpServletResponse);
    if (str == null)
      return;
    SessionIDInfor localSessionIDInfor = SessionDealWith.getSessionIDInfor(str);
    if (localSessionIDInfor == null)
      return;
    ObjectOutputStream localObjectOutputStream = new ObjectOutputStream(paramHttpServletResponse.getOutputStream());
    ArrayList localArrayList = new ArrayList();
    PageSet localPageSet = localSessionIDInfor.getPrintPreviewPageSet4Traversing();
    for (int i = 0; i < localPageSet.size(); ++i)
    {
      ReportPage localReportPage = localPageSet.getPage(i);
      if (localReportPage == null)
        break;
      localArrayList.add(localReportPage);
    }
    localObjectOutputStream.writeObject(localArrayList.toArray(new ReportPage[0]));
    i = 0;
    localObjectOutputStream.writeObject(new Boolean(i));
    localObjectOutputStream.flush();
    localObjectOutputStream.close();
  }

  private static void dealAppletLoad(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = SessionDealWith.getOrGenerateSessionID(paramHttpServletRequest, paramHttpServletResponse);
    if (str == null)
      return;
    Map localMap = WebUtils.createTemplateMap(paramHttpServletRequest);
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("<OBJECT ID='AppletPrinter' name='AppletPrinter' WIDTH='0' HEIGHT='0' CLASSID='clsid:8AD9C840-044E-11D1-B3E9-00805F499D93'");
    localStringBuffer.append(" codebase=\"" + localMap.get("serverURL") + localMap.get("contextPath") + "/jre.exe\">");
    localStringBuffer.append("<param name='code' value='com.fr.base.print.PrintApplet'>");
    localStringBuffer.append("<param name='archive' value='" + localMap.get("serverURL") + localMap.get("contextPath") + "/fr-applet-" + Consts.Main_Version + "." + Consts.Minor_Version + ".jar'>");
    localStringBuffer.append("<param name='sessionid' value='" + str + "'>");
    localStringBuffer.append("<param name='url' value='" + localMap.get("serverURL") + paramHttpServletRequest.getContextPath() + "/" + FRContext.getConfigManager().getServletMapping() + "?op=fr_applet_print&sessionID=" + str + "'>");
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    localPrintWriter.print(localStringBuffer);
    localPrintWriter.flush();
    localPrintWriter.close();
  }
}